package openapi.daum.img;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * @author Administrator
 */
public class OpenImg
{

	/**
	 * @param queryStr
	 * @param pageNo
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	private Document apiQuery(String queryStr, int pageNo) throws JDOMException, IOException
	{
		SAXBuilder builder = new SAXBuilder();
		System.out.print(pageNo * 2 + ((pageNo % 17 == 0) ? "%\n" : "% "));
		String url = "http://apis.daum.net/search/image?";
		String apiKey = "apikey=d3d8cdf0a942f513117c81c97dc26b8934d9f611";
		String query = "&q=" + queryStr;
		// String output = "&output=xml";
		String pageno = "&pageno=" + pageNo;
		String result = "&result=20";
		String strUrl = url + apiKey + query + result + pageno;
		// System.out.println(strUrl);
		return builder.build(strUrl);
	}

	static Vector<infoBeans>	vector	= new Vector<infoBeans>();

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws UnsupportedEncodingException
	{

		/*
		 * try{ throw new IOException(); //throw new Exception();
		 * }catch(Exception e){System.out.println(
		 * "====================================================================== "
		 * ); StackTraceElement[] elem = e.getStackTrace(); for ( int i = 0; i <
		 * elem.length; i++ ) System.out.println(e+"\n\tat "+elem[i]);
		 * System.out.println(
		 * "====================================================================== "
		 * ); System.exit(0); }
		 */
		OpenImg me = new OpenImg();
		infoBeans ib;

		String input = "전지현";
		input = JOptionPane.showInputDialog(" 검색어 입력 ");
		input = URLEncoder.encode(input, "utf-8");
		// System.out.println(input);

		long start = System.currentTimeMillis();
		try
		{
			for (int i = 1; i < 51; i++)
			{
				Document doc = me.apiQuery(input, i);

				doc.getRootElement();
				Element root = doc.getRootElement();
				Element channel = root.getChild("channel".toLowerCase());
				List<Element> item = channel.getChildren("item".toLowerCase());
				for (int j = 0; j < item.size(); j++)
				{
					Element itemInfo = item.get(j);
					ib = new infoBeans();

					ib.setAuthor(itemInfo.getChildTextTrim("author".toLowerCase()));
					ib.setCp(itemInfo.getChildTextTrim("cp".toLowerCase()));
					ib.setHeight(itemInfo.getChildTextTrim("height".toLowerCase()));
					ib.setImage(itemInfo.getChild("image".toLowerCase()).getChildTextTrim("url"));
					ib.setLink(itemInfo.getChildTextTrim("link".toLowerCase()));
					ib.setPubDate(itemInfo.getChildTextTrim("pubData".toLowerCase()));
					ib.setThumbnail(itemInfo.getChildTextTrim("thumbnail".toLowerCase()));
					ib.setTitle(itemInfo.getChildTextTrim("title".toLowerCase()));
					ib.setWidth(itemInfo.getChildTextTrim("width".toLowerCase()));

					vector.add(ib);
				}
			}
			System.out.println();

			BufferedWriter bw = new BufferedWriter(new FileWriter("img.html"));

			bw.append("<head>\n");
			bw.append("<STYLE>\n	.ttip {border:1px solid black;font-size:12px;layer-background-color:lightgreen;background-color:lightgreen}\n</STYLE>\n");

			bw.append("<SCRIPT LANGUAGE=\"JavaScript\">\n");
			bw.append("function showtip(current,e,bogImg){\n");
			// bw.append("	alert(bogImg);\n");
			bw.append("	if (document.layers){\n");
			bw.append("		theString=\"<DIV CLASS='ttip'>\"+bogImg+\"</DIV>\"\n");
			bw.append("		document.tooltip.document.write(theString)\n");
			bw.append("		document.tooltip.document.close()\n");
			bw.append("		document.tooltip.left=e.pageX+14\n");
			bw.append("		document.tooltip.top=e.pageY+2\n");
			bw.append("		document.tooltip.visibility=\"show\"\n");
			bw.append("	}else{\n");
			bw.append("		if(document.getElementById){\n");
			bw.append("			elm=document.getElementById(\"tooltip\")\n");
			bw.append("			elml=current\n");
			bw.append("			elm.innerHTML=bogImg\n");
			bw.append("			elm.style.height=elml.style.height\n");
			bw.append("			elm.style.top=parseInt(elml.offsetTop+elml.offsetHeight)\n");
			bw.append("			elm.style.left=parseInt(elml.offsetLeft+elml.offsetWidth+10)\n");
			bw.append("			elm.style.visibility = \"visible\"\n");
			bw.append("		}\n");
			bw.append("	}\n");
			bw.append("}\n");

			bw.append("function hidetip(){\n");
			bw.append("	if(document.layers){\n");
			bw.append("		document.tooltip.visibility=\"hidden\"\n");
			bw.append("	}else{\n");
			bw.append("		if(document.getElementById){\n");
			bw.append("			elm.style.visibility=\"hidden\"\n");
			bw.append("		}\n");
			bw.append("	}\n");
			bw.append("}\n");

			bw.append("</script>\n");
			bw.append("</head>\n");

			bw.append("<body>\n");
			bw.append("<div id=\"tooltip\" style=\"position:absolute;visibility:hidden;border:0px solid black;font-size:12px;layer-background-color:lightyellow;background-color:lightyellow;padding:0px\"></div>\n");

			for (int i = 0; i < vector.size(); i++)
			{

				bw.write("<a href=\"" + vector.get(i).getLink() + "\" onMouseover=\"showtip(this,event,'<img src=" + vector.get(i).getImage() + ">')\" onMouseOut=\"hidetip()\"><img src='" + vector.get(i).getThumbnail()
						+ "' border='0'></a> ");
				bw.newLine();
			}
			bw.append("</body>\n");

			bw.close();



			System.out.println((System.currentTimeMillis() - start) + " millisecond");
		}
		catch (JDOMException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

}
